package com.midea.ai.air.ui.chat;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.midea.ai.air.R;

import java.util.List;

/**
 * Created by ouyangdi on 2015/12/30.
 */
public class ChatlistAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<ChatMsgModel> mChatMsgs;

    Resources mResources;

    public ChatlistAdapter(Context context, List<ChatMsgModel> chatMsgs) {
        mInflater = LayoutInflater.from(context);
        mChatMsgs = chatMsgs;
        mResources = context.getResources();
    }

    @Override
    public Object getItem(int position) {
        return mChatMsgs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return mChatMsgs.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMsgModel item = mChatMsgs.get(position);
        ViewHolder holder =null;
        ViewHolderVoice holderVoice = null;
        int type = getItemViewType(position);

        if(convertView == null) {
            if(type == 0){
                convertView = mInflater.inflate(R.layout.item_receive_msg,parent,false);
                holder = new ViewHolder();
                holder.msg = (TextView)convertView.findViewById(R.id.tv_re_text);
                convertView.setTag(holder);
            } else if(type == 1) {
                convertView = mInflater.inflate(R.layout.item_send_msg,parent,false);
                holder = new ViewHolder();
                holder.msg = (TextView)convertView.findViewById(R.id.tv_send_txt);
                convertView.setTag(holder);
            } else {
                convertView = mInflater.inflate(R.layout.item_send_voice,parent,false);
                holderVoice = new ViewHolderVoice();
                holderVoice.msg = convertView.findViewById(R.id.tv_send_voice);
                holderVoice.time = (TextView)convertView.findViewById(R.id.tv_msg_time);
                convertView.setTag(holderVoice);
            }

        }

        else {
            if(type == 2) {
                holderVoice = (ViewHolderVoice) convertView.getTag();
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
        }

        if(type == 2) {
              /**显示消息时长、根据时长调整消息框的长度*/
        }
        else{
            holder.msg.setText(item.getMsg());
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMsgModel item = mChatMsgs.get(position);
        if(item.getType() == ChatMsgModel.Type.RECEIVE) {
            return 0;
        } else if(item.getType() == ChatMsgModel.Type.SEND_TEXT) {
            return 1;
        }
        return 2;
    }

    public class ViewHolder {
        TextView msg;
    }

    public class ViewHolderVoice {
        View msg; //消息框
        TextView time; //消息时长
    }
}
